package com.jinjiafeng.baselibrary.widget.recycleview;

import android.support.annotation.IntRange;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.Collection;
import java.util.List;

/**
 * @author jinjiafeng
 * Time :18-6-10
 */
public abstract class BaseRecycleAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> {

    private final List<T> mDataList;
    private int mHeaderKey = 100000;
    private int mFooterKey = 200000;
    private LayoutInflater mLayoutInflater;
    private int mLayoutId;
    private OnItemClickListener mOnItemClickListener;
    /**
     * 支持多布局的条目
     */
    private BaseMultiSupport<T> mMultiSupportItem;
    private SparseArray<View> mHeaderViews = new SparseArray<>();
    private SparseArray<View> mFooterViews = new SparseArray<>();


    public BaseRecycleAdapter(List<T> data, BaseMultiSupport<T> multiSupportItem) {
        this(-1, data);
        this.mMultiSupportItem = multiSupportItem;
    }

    public BaseRecycleAdapter(@LayoutRes int layoutId, List<T> data) {
        this.mDataList = data;
        this.mLayoutId = layoutId;
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //如果是头布局
        if (mHeaderViews.indexOfKey(viewType) >= 0) {
            return createHeaderFooterHolder(mHeaderViews.get(viewType));
            //如果是脚布局
        } else if (mFooterViews.indexOfKey(viewType) >= 0) {
            return createHeaderFooterHolder(mFooterViews.get(viewType));
        }
        //创建内容的ViewHolder
        return createBaseViewHolder(parent, viewType);
    }

    private BaseViewHolder createHeaderFooterHolder(View view) {
        return new BaseViewHolder(view);
    }

    @NonNull
    private BaseViewHolder createBaseViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (mLayoutInflater == null) {
            this.mLayoutInflater = LayoutInflater.from(parent.getContext());
        }
        //如果支持多布局 viewType->为getItemViewType()返回的值
        if (mMultiSupportItem != null) {
            mLayoutId = viewType;
        }
        final View view = mLayoutInflater.inflate(mLayoutId, parent, false);
        return new BaseViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final BaseViewHolder holder, int position) {
        //如果当前是头布局
        if (position < getHeadersCount()) {
            return;
        }
        //获取集合的索引
        int adjPosition = position - getHeadersCount();
        int adapterCount = mDataList.size();
        if (adjPosition < adapterCount) {
            convert(holder, mDataList.get(adjPosition));
            if (mOnItemClickListener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        final int adapterPosition = holder.getAdapterPosition();
                        mOnItemClickListener.onItemClick(adapterPosition - getHeadersCount());
                    }
                });
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        int numHeaders = getHeadersCount();
        if (position < numHeaders) {
            return mHeaderViews.keyAt(position);
        }
        int adjPosition = position - numHeaders;
        int adapterCount = 0;
        if (position >= numHeaders) {
            adapterCount = mDataList.size();
            if (adjPosition < adapterCount) {
                if (mMultiSupportItem != null) {
                    //将内容传递出去,根据item中字段获得不同的layoutId,
                    return mMultiSupportItem.getLayoutId(mDataList.get(adjPosition));
                }
                //这个方法在onCreateViewHolder(ViewGroup parent, int viewType)之前调用,getItemViewType
                // 返回值就是onCreateViewHolder中的viewType->即多布局
                return super.getItemViewType(adjPosition);
            }
        }
        //返回mFooterViews集合中的key值
        return mFooterViews.keyAt(adjPosition - adapterCount);
    }

    @Override
    public int getItemCount() {
        return mDataList.size() + getHeadersCount() + getFootersCount();
    }

    public int getFootersCount() {
        return mFooterViews.size();
    }

    public int getHeadersCount() {
        return mHeaderViews.size();
    }

    /**
     * 将数据显示到布局上
     *
     * @param holder BaseViewHolder
     * @param data   数据
     */
    public abstract void convert(BaseViewHolder holder, T data);

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    /**
     * 添加脚布局
     *
     * @param view view
     */
    public void addHeaderView(View view) {
        if (mHeaderViews.indexOfValue(view) == -1) {
            mHeaderViews.put(mHeaderKey++, view);
            final int indexOfValue = mHeaderViews.indexOfValue(view);
            notifyItemInserted(indexOfValue);
        }
    }


    /**
     * 添加脚布局
     *
     * @param view view
     */
    public void addFooterView(View view) {
        //没有添加过,才进行添加
        if (mFooterViews.indexOfValue(view) == -1) {
            mFooterViews.put(mFooterKey++, view);
            final int indexOfValue = mFooterViews.indexOfValue(view);
            notifyItemInserted(getHeadersCount() + mDataList.size() + indexOfValue);
        }

    }

    /**
     * 移除头布局
     *
     * @param view 要移除的view
     */
    public void removeHeaderView(View view) {
        final int index = mHeaderViews.indexOfValue(view);
        if (index >= 0) {
            mHeaderViews.removeAt(index);
            notifyItemRemoved(index);
        }
    }

    /**
     * 移除脚布局
     *
     * @param view 要移除的view
     */
    public void removeFooterView(View view) {
        final int index = mFooterViews.indexOfValue(view);
        if (index >= 0) {
            mFooterViews.removeAt(index);
            notifyItemRemoved(getHeadersCount() + mDataList.size() + index);
        }
    }

    /**
     * add new data to the end of mData
     *
     * @param newData the new data collection
     */
    public void addData(@NonNull Collection<? extends T> newData) {
        mDataList.addAll(newData);
        notifyItemRangeInserted(mDataList.size() - newData.size() + getHeadersCount(), newData
                .size());
    }

    /**
     * remove the item associated with the specified position of adapter
     *
     * @param position 要删除的position
     */
    public void remove(@IntRange(from = 0) int position) {
        mDataList.remove(position);
        int internalPosition = position + getHeadersCount();
        notifyItemRemoved(internalPosition);
        notifyItemRangeChanged(internalPosition, mDataList.size() - internalPosition);
    }

}
